package com.yidu.hbatsft.action;

import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yidu.hbatsft.domain.*;
import com.yidu.hbatsft.service.AtsBaseSportService;
import com.yidu.hbatsft.service.AtsEmpCoachService;
import com.yidu.hbatsft.service.AtsPlanItemService;
import com.yidu.hbatsft.service.AtsPlanPhaseService;
import com.yidu.privilege.Utils.BaseMessage;
import com.yidu.utils.FileTools;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/atsPhase")
public class AtsPlanPhaseController {
    @Resource
    AtsPlanPhaseService atsPlanPhaseService;//阶段计划

    @Resource
    AtsBaseSportService atsBaseSportService;// 体育项目管理

    @Resource
    AtsEmpCoachService atsEmpCoachService;// 教练员

    @Resource
    AtsPlanItemService atsPlanItemService;//指标

    @Resource
    FileTools fileTools;//文件上传

    @RequestMapping("selectAll")
    @ResponseBody
    public Map<String,Object> selectAll(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize, AtsPlandimSearch atsPlandimSearch){
        Map<String, Object> map = new HashMap<String, Object>();
        List<AtsPlanPhase> list = atsPlanPhaseService.selectAll(pageNum,pageSize,atsPlandimSearch);//分页数据
        Integer count = atsPlanPhaseService.count(atsPlandimSearch);

        map.put("code", 0);
        map.put("msg", "");
        map.put("msg", "");
        map.put("count", count);
        map.put("data",list);

        return map;
    }

    @RequestMapping("selMessage")
    @ResponseBody
    public Map<String,Object> selMessage(){

        Map<String, Object> map = new HashMap<String, Object>();
        //所有运动项目名
        List<AtsBaseSport> atsBaseSport = atsBaseSportService.selAllSportNmae();
        //所有教练员的姓名
        List<AtsEmpCoach> atsEmpCoach = atsEmpCoachService.selAllCoachName();
        //指标
        List<AtsPlanItem> atsPlanItem = atsPlanItemService.selectItem("3");

        map.put("sport",atsBaseSport);
        map.put("coach",atsEmpCoach);
        map.put("item",atsPlanItem);

        return map;
    }

    @RequestMapping("addOrUpdate")
    @ResponseBody
    public BaseMessage addOrUpdate(AtsPlanPhase atsPlanPhase, AtsPlanItem atsPlanItem ,String dateTime , String itemIdArry, MultipartFile file1s, MultipartFile file2s, MultipartFile file3s) throws ParseException {
        System.out.println("-------------"+dateTime);
        BaseMessage bm = new BaseMessage();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //2. 调用parse方法，将字符串转成Date对象
        Date date = sdf.parse(dateTime);

        System.out.println("-------------"+date);
        atsPlanPhase.setPhaseDate(date);
        atsPlanPhase.setCreatTime(new Date());//设置创建时间
        atsPlanPhase.setCreater("sb");//设置当前登陆人

        if (!"".equals(file1s.getOriginalFilename()) && file1s.getOriginalFilename() != null){
            try {
                String file1 = fileTools.saveFile("images",file1s);
                atsPlanPhase.setFile1(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            atsPlanPhase.setFile1(null);
        }
        if (!"".equals(file2s.getOriginalFilename()) && file2s.getOriginalFilename() != null){
            try {
                String file2 = fileTools.saveFile("images",file2s);
                atsPlanPhase.setFile2(file2);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            atsPlanPhase.setFile2(null);
        }
        if (!"".equals(file3s.getOriginalFilename()) && file3s.getOriginalFilename() != null){
            try {
                String file3 = fileTools.saveFile("images",file3s);
                atsPlanPhase.setFile3(file3);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            atsPlanPhase.setFile3(null);
        }


        atsPlanItemService.itemUpdte("3");//将上一次指标都改为0

        AtsBaseSport atsBaseSport = atsBaseSportService.selSportNmaeLX(atsPlanPhase.getSportId());//查询出atsPlanYear的item名
        atsPlanPhase.setItemName(atsBaseSport.getSportName());

        Integer temp = null;
        System.out.println("id:"+atsPlanPhase.getPhaseId());
        if(atsPlanPhase.getPhaseId() != 0){
            temp = atsPlanPhaseService.updateByPrimaryKeySelective(atsPlanPhase,atsPlanItem,itemIdArry);

        }else{
            temp = atsPlanPhaseService.insertSelective(atsPlanPhase,atsPlanItem);
        }
        if(temp > 0){
            bm.setState(1);
            bm.setMsg("操作成功");
        }else {
            bm.setState(0);
            bm.setMsg("操作失败");
        }
        return bm;
    }

    @RequestMapping("updSel")
    @ResponseBody
    public Map<String,Object> selectByPrimaryKey(Integer phaseId){
        AtsPlanPhase atsPlanPhase = null;
        List<AtsPlanItem> atsPlanItem = null;
        Map<String, Object> map = null;
        if (phaseId != null && !"".equals(phaseId)){
            map = new HashMap<String, Object>();
            atsPlanPhase = atsPlanPhaseService.selectByPrimaryKey(phaseId);
            atsPlanItem = atsPlanItemService.selectItemByPid(phaseId.toString(),"3");

            List<String> list = null;
            if(atsPlanPhase.getOtherCoach() != null && !"".equals(atsPlanPhase.getOtherCoach())) {
                String otherNameTemp[] = atsPlanPhase.getOtherCoach().split(",");
                list = new ArrayList<String>();
                if (otherNameTemp[0] != null && !"".equals(otherNameTemp[0])) {
                    for (int i = 0; i < otherNameTemp.length; i++) {
                        String otherName = otherNameTemp[i];
                        AtsEmpCoach atsEmpCoach = atsEmpCoachService.selCoachId(otherName);
                        list.add(atsEmpCoach.getCoachId().toString());
                    }
                } else {
                    list.add(null);
                }
            }
            map.put("YearMore",atsPlanPhase);
            map.put("Item",atsPlanItem);
            map.put("otherNameId",list);
        }

        return map;
    }

    @RequestMapping("Delete")
    @ResponseBody
    public BaseMessage updateDelete(String ids){
        BaseMessage bm = new BaseMessage();
        Integer temp = atsPlanPhaseService.updateDelete(ids);
        if(temp > 0){
            bm.setState(1);
            bm.setMsg("删除成功");
        }else {
            bm.setState(0);
            bm.setMsg("删除失败");
        }
        return bm;
    }
}
